//
//  EJSOrderListViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/3.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSOrderListViewController.h"
#import "EJSOrderListRequest.h"
#import "EJSOrderDetailViewController.h"

//#import "EJSOrderStatusTableViewCell.h"
//#import "EJSOrderGoodsTableViewCell.h"
//#import "EJSOrderOperationTableViewCell.h"
#import "XOCartShopTableCell.h"
#import "XOShoppingCartModel.h"
@interface EJSOrderListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSInteger _currentPage;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation EJSOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _currentPage = 1;
    [self getOrderData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    self.isNeedPopToRoot = YES;
//    self.view.backgroundColor = UIColorFromRGB(0xDCDCDC);
    if(!self.orderType){
        self.title = @"全部订单";
    }else if (self.orderType.integerValue == 1){
        self.title = @"已下单";
    }else if (self.orderType.integerValue == 2){
        self.title = @"待发货";
    }else if (self.orderType.integerValue == 3){
        self.title = @"待收货";
    }else if (self.orderType.integerValue == 4){
        self.title = @"已完成";
    }else if (self.orderType.integerValue == 5){
        self.title = @"已评价";
    }
    if([self.refoundFlag isEqualToString:@"refound"]){
        self.title = @"退款/退货";
    }
    
    self.tableView.rowHeight = 130;
//    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderStatusTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderStatusTableViewCell"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderGoodsTableViewCell"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"EJSOrderOperationTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSOrderOperationTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"XOCartShopTableCell" bundle:nil] forCellReuseIdentifier:@"XOCartShopTableCell_order"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [self getOrderData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self getOrderData];
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

- (void)getOrderData{
    EJSOrderListRequest *req = [EJSOrderListRequest new];
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"500";
    req.flag = self.orderType;
    req.refoundFlag = self.refoundFlag;
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        EJSOrderListModel *listModel = response.data;
        if(listModel.totalPages.integerValue == listModel.page.integerValue){
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        if(_currentPage == 1){
            weakSelf.dataSource = [NSMutableArray arrayWithArray:listModel.list];
        }else{
            [weakSelf.dataSource addObjectsFromArray:listModel.list];
        }
        if(weakSelf.dataSource.count == 0){
            [weakSelf showNoDataViewWithText:@"没有相关订单哦"];
        }else{
            [weakSelf hideNoDataView];
        }
        [weakSelf.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

#pragma mark TableView DataSource
//- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
//    return self.dataSource.count;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XOCartShopTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XOCartShopTableCell_order" forIndexPath:indexPath];
    XOShoppingCartModel *shopModel = [XOShoppingCartModel new];
    EJSOrderModel* data = self.dataSource[indexPath.row];
    shopModel.totalAmount = @(data.totalAmount.doubleValue);
    shopModel.logo = data.shop.logo;
    shopModel.shopName = data.shop.shopName;
    shopModel.minBuyMomeny = @([data.shop.minBuyMomeny floatValue]/100.0).stringValue;
    cell.shopModel = shopModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EJSOrderModel *orderModel = self.dataSource[indexPath.row];
    EJSOrderDetailViewController *orderDetailVC = [[EJSOrderDetailViewController alloc] initWithNibName:@"EJSOrderDetailViewController" bundle:nil];
    orderDetailVC.orderModel = orderModel;
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

@end
